"""
质量评估服务模块
"""

from datetime import datetime
from typing import Optional
from loguru import logger

from ..models.api_model import QualityEvaluationRequest, QualityEvaluationResponse


class QualityService:
    """质量评估服务类"""

    def __init__(self):
        self.quality_thresholds = {
            "coherence": 0.7,
            "creativity": 0.6,
            "educational_value": 0.8,
            "language_quality": 0.8
        }

    async def evaluate_story_quality(self, story_content: str, story_id: Optional[int] = None) -> QualityEvaluationResponse:
        """评估故事质量"""
        try:
            # 简化的质量评估逻辑
            coherence_score = self._evaluate_coherence(story_content)
            creativity_score = self._evaluate_creativity(story_content)
            educational_score = self._evaluate_educational_value(story_content)
            language_score = self._evaluate_language_quality(story_content)

            # 计算综合评分
            overall_score = (coherence_score + creativity_score + educational_score + language_score) / 4

            return QualityEvaluationResponse(
                success=True,
                message="质量评估完成",
                story_id=story_id,
                overall_score=overall_score,
                coherence_score=coherence_score,
                creativity_score=creativity_score,
                evaluation_time=datetime.now()
            )

        except Exception as e:
            logger.error(f"质量评估失败: {str(e)}")
            return QualityEvaluationResponse(
                success=False,
                message=f"质量评估失败: {str(e)}",
                evaluation_time=datetime.now()
            )

    def _evaluate_coherence(self, content: str) -> float:
        """评估故事连贯性"""
        # 简化评估逻辑
        if len(content) > 100:
            return 0.8
        return 0.6

    def _evaluate_creativity(self, content: str) -> float:
        """评估故事创意性"""
        # 简化评估逻辑
        if "有趣" in content or "精彩" in content:
            return 0.8
        return 0.6

    def _evaluate_educational_value(self, content: str) -> float:
        """评估教育价值"""
        # 简化评估逻辑
        if "学习" in content or "教育" in content:
            return 0.9
        return 0.7

    def _evaluate_language_quality(self, content: str) -> float:
        """评估语言质量"""
        # 简化评估逻辑
        if len(content) > 200:
            return 0.8
        return 0.6 